package com.ht.controller;

import com.ht.bean.Dep;
import com.ht.bean.Emp;
import com.ht.service.LoginService;
import com.ht.service.StypetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/*
 *用户登录
 * @param
 * @return
 **/
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Resource
    private StypetingService styp;


    @RequestMapping("/emplogin")
    public String gologin(String phone, String password, Model model, HttpSession session){

        Emp emp = loginService.login(phone,password);
        if (emp == null){//如果登录失败
            model.addAttribute("message","账号或密码错误，请重新输入！");
            return "forward:/index.jsp";
        } else  if (emp.getStatus() == 2){
            model.addAttribute("messages","该账号被禁用，请联系管理员");
            return "forward:/index.jsp";
        } else {//登录成功
            //登录员工的数据，
            session.setAttribute("emp",emp);
            //登录员工的id
            session.setAttribute("empID",emp.getEmpId());


            System.out.println(emp.getEmpId());
            //登录员工的名字
            session.setAttribute("empName",emp.getEmpName());
            System.out.println(emp.getEmpName());
            //登录员工的权限
            session.setAttribute("empquan",styp.inquire(emp.getEmpId()));
            System.out.println(emp.getEmpName());
            return "redirect:/login/gomain";
        }
    }
    /*
     *退出登录
     * @param
     * @return
     **/
    @RequestMapping("/goexit")
    public String goexit(){
        return "forward:/index.jsp";
    }

    @RequestMapping("/gomain")
    public String gomain(){
        return "main";
    }

    @RequestMapping("/gohome")
    public String gohome(){
        return "home";
    }


    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("empId");
        session.invalidate();
        return "forward:/index.jsp";
    }



}
